package com.lanswon.carlocationsearch.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.PropertyUtilsBean;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @Description
 * @Author zsw
 * @Date 2019/12/7 17:58
 * @Version V1.0
 **/
@Slf4j
public class ReflectUtil {

    public static Object getTarget(Object dest, Map<String, Object> addProperties) {
        PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean();
        PropertyDescriptor[] descriptors = propertyUtilsBean.getPropertyDescriptors(dest);
        Map<String, Class> propertyMap = new HashMap<>();
        for (PropertyDescriptor d : descriptors) {
            if (!"class".equalsIgnoreCase(d.getName())) {
                propertyMap.put(d.getName(), d.getPropertyType());
            }
        }
        // add extra properties
        addProperties.forEach((k, v) -> propertyMap.put(k, v.getClass()));
        // new dynamic bean
        DynamicBean dynamicBean = new DynamicBean(dest.getClass(), propertyMap);
        // add old value
        propertyMap.forEach((k, v) -> {
            try {
                // filter extra properties
                if (!addProperties.containsKey(k)) {
                    dynamicBean.setValue(k, propertyUtilsBean.getNestedProperty(dest, k));
                }
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        });
        // add extra value
        addProperties.forEach((k, v) -> {
            dynamicBean.setValue(k, v);
        });

        Object target = dynamicBean.getTarget();
        return target;
    }


    public static void main(String[] args) throws Exception {

    }




    private static Object dynamicClass(Object object, Map<String, Object> moneyMap) throws Exception {
        // 字段 - 值 集合
        HashMap<String, Object> returnMap = new HashMap<>();
        // 字段 - 字段类型 集合
        HashMap<String, Class> typeMap = new HashMap<>();
        // 获取传入类
        Class<? extends Object> type = object.getClass();
        BeanInfo beanInfo = Introspector.getBeanInfo(type);
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        // 获取对象中已存在的数据
        for (int i = 0; i < propertyDescriptors.length; i++) {
            PropertyDescriptor descriptor = propertyDescriptors[i];
            String propertyName = descriptor.getName();
            if (!propertyName.equals("class") && !propertyName.equals("monthMap")) {
                Method readMethod = descriptor.getReadMethod();
                Object result = readMethod.invoke(object, new Object[0]);
                if (result != null) {
                    returnMap.put(propertyName, result);
                } else {
//                    String propertyType = descriptor.getPropertyType().toString();

                        returnMap.put(propertyName, "");

                }
                typeMap.put(propertyName, descriptor.getPropertyType());
            }
        }
        // 获取月份数据， 变为字段属性
        Set<String> monthKeys = moneyMap.keySet();
        for (Iterator<String> it = monthKeys.iterator(); it.hasNext();) {
            String key = (String) it.next();
            // 字段类型
            typeMap.put(key, moneyMap.get(key).getClass());
            // 字段对应值
            returnMap.put(key, moneyMap.get(key));
        }
        // map转换成实体对象
        DynamicBean bean = new DynamicBean(typeMap);
        // 赋值
        Set<String> keys = typeMap.keySet();
        for (Iterator<String> it = keys.iterator(); it.hasNext();) {
            String key = (String) it.next();
            bean.setValue(key, returnMap.get(key));
        }

        System.out.println(bean.getValue("mm"));
        Object obj = bean.getTarget();
        return obj;
    }



}